package com.apexsoft.monitor.front.console.app.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.annotation.CrudMethod;
import com.apexsoft.monitor.front.console.aop.AopAnnotation;
import com.apexsoft.monitor.front.console.app.service.AppNodeService;
import com.apexsoft.monitor.front.console.pageModel.PappNode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("appNodeController")
@RequestMapping("/console/appNode")
public class AppNodeController extends BaseFrontController {
	
	@Autowired
	@Qualifier("appNodeService")
	private AppNodeService appNodeService = null;


	/**
	 * 节点列表(分页查询)
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/listByPage")
	public String listByPage(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "console/appNode/list";
		
	}
	
	@RequestMapping(value = "/loadNodeByPage")
	public String getEsbNodes(HttpServletRequest request, HttpServletResponse response,@ModelAttribute("form") PappNode pappNode) throws Exception {
		//Pagination page =new Pagination(pappNode.getPageNum(),pappNode.getPageSize());
		JSONObject result = this.appNodeService.listByPage(pappNode);
		this.printJSON(response, result);
		return null;
		
	}
	@CrudMethod
	@AopAnnotation(description="添加应用节点")
	@RequestMapping(value = "/add")
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		JSONObject result = appNodeService.add(request);
		this.printJSON(response, result);
		return null;
		
	}

	@CrudMethod
	@AopAnnotation(description="修改应用节点信息")
	@RequestMapping(value = "/update")
	public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		JSONObject result = appNodeService.update(request);
		this.printJSON(response, result);
		return null;
		
	}

	@CrudMethod
	@AopAnnotation(description="根据nodeCode删除节点")
	@RequestMapping(value = "/delete")
	public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String nodeCode = request.getParameter("nodeCode");
		String hostCode = request.getParameter("hostCode");
		JSONObject result = appNodeService.delete(nodeCode,hostCode);
		this.printJSON(response, result);
		return null;

	}

	@RequestMapping(value = "/getAllNode")
	public String getAllNode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONArray array = new JSONArray();
		String appCode = request.getParameter("appCode");
		if (StringUtils.isNotBlank(appCode)){
			JSONObject result = appNodeService.getAllNode(appCode);
			if (result != null && result.getIntValue(Result.CODE) > 0) {
				JSONObject nullObj = new JSONObject();
				nullObj.put("nodeCode","");
				nullObj.put("nodeName","----请选择----");
				array.add(nullObj);
				array.addAll(1,result.getJSONArray(Result.ROWS));
			}
		}else {
			JSONObject nullObj = new JSONObject();
			nullObj.put("nodeCode","");
			nullObj.put("nodeName","----请选择----");
			array.add(nullObj);
		}
		this.printJSON(response, array);
		return null;
	}

	/**
	 * 获取中间件,不包括选择项
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
     */
	@RequestMapping(value = "/getAllNodeNoSelect")
	public String getAllNodeNoSelect(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONArray array = new JSONArray();
		String appCode = request.getParameter("appCode");
		if (StringUtils.isNotBlank(appCode)){
			JSONObject result = appNodeService.getAllNode(appCode);
			if (result != null && result.getIntValue(Result.CODE) > 0) {
				array.addAll(result.getJSONArray(Result.ROWS));
			}
		}else {
			JSONObject nullObj = new JSONObject();
			nullObj.put("nodeCode","");
			nullObj.put("nodeName","----请选择----");
			array.add(nullObj);
		}
		this.printJSON(response, array);
		return null;

	}

	/**
	 * 获取所有中间件,实时日志专用,包括主机状态和中间件状态
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
     */
	@RequestMapping(value = "/getAllNodeForLog")
	public String getAllNodeForLog(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONArray array = new JSONArray();
		String appCode = request.getParameter("appCode");
		if (StringUtils.isNotBlank(appCode)){
			JSONObject result = appNodeService.getAllNodeForLog(appCode);
			if (result != null && result.getIntValue(Result.CODE) > 0) {
				array.addAll(result.getJSONArray(Result.ROWS));
			}
		}else {
			JSONObject nullObj = new JSONObject();
			nullObj.put("nodeCode","");
			nullObj.put("nodeName","----请选择----");
			array.add(nullObj);
		}
		this.printJSON(response, array);
		return null;
	}

	/**
	 * 获取包含应用信息在内的,中间件信息,用于comboboxTree组件的使用数据
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
     */
	@RequestMapping(value = "/getAllNodeForComboBoxTree")
	public String getAllNodeForComboBoxTree(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONArray array = appNodeService.getAllNodeForComboBoxTree();
		this.printJSON(response, array);
		return null;
	}
}
